package com.ygqh.baby.service.impl;
import java.util.List;

import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.po.AdminSupplier;
import com.ygqh.baby.po.AdminSupplierExample.Criteria;
import  com.ygqh.baby.mapper.AdminSupplierMapper;
import  com.ygqh.baby.service.AdminSupplierService;

import  org.springframework.util.Assert;

import java.util.Arrays;

import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;

import  com.ygqh.baby.po.AdminSupplierExample;
import  com.ygqh.baby.ao.DataStatus;

@Service
public class AdminSupplierServiceImpl implements AdminSupplierService{
	
	@Autowired
	private AdminSupplierMapper adminSupplierMapper;
	
	@Override
	public List<AdminSupplier> find() {
		AdminSupplierExample example = new AdminSupplierExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminSupplierMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminSupplierExample example = new AdminSupplierExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminSupplierMapper.countByExample(example);
	}

	@Override
	public AdminSupplier findById(Long id) {
		return adminSupplierMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(AdminSupplier adminSupplier) {
		return adminSupplierMapper.insertSelective(adminSupplier);
	}

	@Override
	public int update(AdminSupplier adminSupplier) {
		return adminSupplierMapper.updateByPrimaryKeySelective(adminSupplier);
	}

	@Override
	public ResultSet<AdminSupplier> search(QueryInfo queryInfo,String q) {
		List<AdminSupplier>  list =  adminSupplierMapper.selectSuper(queryInfo, q);
		int count = adminSupplierMapper.countSuper(q);
		ResultSet<AdminSupplier> resultSet = new ResultSet<AdminSupplier>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminSupplierExample example = new AdminSupplierExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminSupplier record = new AdminSupplier();
		record.setStatus(DataStatus.Delete);

		return adminSupplierMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(AdminSupplier adminSupplier) {
		int r = 0;
		if(adminSupplier.getId() != null) {
			r = this.update(adminSupplier);
		}else {
			r = this.save(adminSupplier);
		}
		return r;
	}
	@Override
	public List<AdminSupplier> find(String q){
		AdminSupplierExample example = new AdminSupplierExample();
		example.setOrderByClause("id desc");
		Criteria criteria=example.createCriteria();		
		criteria.andStatusEqualTo(DataStatus.Valid.name());
		
		if(q!=null)
			criteria.andSupplierNameLike(q);
		return adminSupplierMapper.selectByExample(example);
	}

	@Override
	public int checkSupplierName(String supplierName, Long id) {
		return adminSupplierMapper.checkSupplierName(supplierName,id);
		
	}

}

